//
//  ListViewManager.m
//  Task Views
//
//  Created by Chris Karr on 2/18/09.
//  Copyright 2009 Chris J. Karr. All rights reserved.
//

#import "ListViewManager.h"
#import "AttributeValueFormatter.h"

@implementation ListViewManager

- (void) saveColumns
{
	NSMutableArray * columns = [NSMutableArray array];
	
	for (NSTableColumn * column in [listTable tableColumns])
	{
		NSMutableDictionary * dict = [NSMutableDictionary dictionary];
		[dict setValue:[column identifier] forKey:@"identifier"];
		[dict setValue:[NSNumber numberWithFloat:[column width]] forKey:@"width"];
		
		[columns addObject:dict];
	}
	
	[[NSUserDefaults standardUserDefaults] setObject:columns forKey:@"list_table_columns"];
}

- (void) restoreColumns
{
	for (NSTableColumn * column in [NSArray arrayWithArray:[listTable tableColumns]])
		[listTable removeTableColumn:column];
	
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	NSArray * columns = [defaults objectForKey:@"list_table_columns"];
	
	AttributeValueFormatter * formatter = [[AttributeValueFormatter alloc] init];
	
	for (NSDictionary * dict in columns)
	{
		NSString * key = [dict valueForKey:@"identifier"];
		
		NSTableColumn * column = [[NSTableColumn alloc] initWithIdentifier:key];
		
		[column bind:@"value" toObject:tasks withKeyPath:[NSString stringWithFormat:@"arrangedObjects.%@", key, nil] 
			 options:[NSDictionary dictionary]];
		[[column headerCell] setStringValue:key];
		
		[listTable addTableColumn:column];

		[column setWidth:[[dict valueForKey:@"width"] floatValue]];
		[[column dataCell] setFormatter:formatter];
		[column release];
	}
	
	[formatter release];
}

- (void) updateColumns
{
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	
	NSArray * oldColumns = [defaults valueForKey:@"list_table_columns"];
	if (oldColumns == nil)
		oldColumns = [NSArray array];

	NSMutableArray * newColumns = [NSMutableArray array];

	NSDictionary * listColumns = [defaults objectForKey:@"list_columns"];
	if (listColumns == nil)
		listColumns = [NSDictionary dictionary];
	
	for (NSDictionary * dict in oldColumns)
	{
		for (NSString * key in [listColumns allKeys])
		{
			if ([[listColumns valueForKey:key] boolValue])
			{
				if ([[dict valueForKey:@"identifier"] isEqual:key])
					[newColumns addObject:dict];
			}
		}
	}
	
	for (NSString * key in [listColumns allKeys])
	{
		if ([[listColumns valueForKey:key] boolValue])
		{
			BOOL found = NO;
			
			for (NSDictionary * dict in oldColumns)
			{
				if ([[dict valueForKey:@"identifier"] isEqual:key])
					found = YES;
			}
			
			if (!found)
			{
				NSMutableDictionary * dict = [NSMutableDictionary dictionary];
				[dict setValue:key forKey:@"identifier"];
				[dict setValue:[NSNumber numberWithFloat:100] forKey:@"width"];
				
				[newColumns addObject:dict];
			}
		}
	}

	[defaults setObject:newColumns forKey:@"list_table_columns"];
	[self restoreColumns];
}

- (void) awakeFromNib
{
	[listTable setDelegate:self];
	[listTable setTarget:taskManager];
	[listTable setDoubleAction:@selector(editTask:)];
	
	[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"list_columns" options:0 context:NULL];
	[self restoreColumns];
}

- (void) observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
	[self updateColumns];
}
	
- (IBAction) settings:(id) sender
{
	[NSApp beginSheet:settings modalForWindow:listView modalDelegate:self didEndSelector:nil contextInfo:NULL];
}

- (IBAction) closeSettings:(id) sender;
{
	[NSApp endSheet:settings];
	[settings orderOut:self];
}

- (void)tableViewColumnDidMove:(NSNotification *)aNotification
{
	NSLog (@"move");
	[self saveColumns];
}

- (void)tableViewColumnDidResize:(NSNotification *)aNotification
{
	NSLog (@"resize");
	[self saveColumns];
}

@end
